/*
 * Copyright (c) 2011 Team A CS2340. All rights reserved.
 */

package edu.gatech.teama.doctorsoffice.client.view;

import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;


import net.miginfocom.swing.MigLayout;
import java.awt.Component;
import javax.swing.Box;

import edu.gatech.teama.doctorsoffice.client.presenter.ServerConnection;
import edu.gatech.teama.doctorsoffice.model.Doctor;
import edu.gatech.teama.doctorsoffice.model.Nurse;
import edu.gatech.teama.doctorsoffice.model.Patient;
import edu.gatech.teama.doctorsoffice.model.SystemAdmin;
import java.awt.SystemColor;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * MainWindow.java
 * @author unknown
 * @version 1.0
 */
//TODO re-enable audit
//$codepro.audit.disable
public class MainWindow {

	/**
	 * frame
	 */
	private static JFrame frame;
	
	/**
	 * currentComponent
	 */
	private static Component currentComponent;
	/**
	 * @wbp.nonvisual location=417,121
	 */
	private final Component glue = Box.createGlue();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				ServerConnection.getInstance().disconnect();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		frame.getContentPane().setBackground(SystemColor.activeCaption);
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new MigLayout("", "[grow][440px][grow]", "[grow][270px][grow]"));
		
		//frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		JPanel panelLogin = new LoginPanel();
		panelLogin.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		currentComponent = panelLogin;
		frame.getContentPane().add(panelLogin, "flowx,cell 1 1,grow");
		
		Component glue_1 = Box.createGlue();
		frame.getContentPane().add(glue_1, "cell 2 1");
		
		Component glue_2 = Box.createGlue();
		frame.getContentPane().add(glue_2, "cell 1 2");
		//frame.setContentPane(panelLogin);
		//frame.getContentPane().add(panelLogin, "cell 0 0,grow");
	}
	
	/**
	 * returns frame
	 */
	public static JFrame getFrame() {
		return frame;
	}
	
	/**
	 * setPanel
	 * @param panel
	 */
	public static void setPanel(JPanel panel) {
		boolean wasMaximized = (frame.getExtendedState() & Frame.MAXIMIZED_BOTH) > 1;
		frame.getContentPane().remove(currentComponent);
		panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		frame.getContentPane().add(panel, "flowx,cell 1 1,grow");
		currentComponent = panel;
		frame.pack();
		if (wasMaximized) // remaximize
			frame.setExtendedState(frame.getExtendedState() | Frame.MAXIMIZED_BOTH);
	}
	
	public static void backToMainPanel() {
		ServerConnection sc = ServerConnection.getInstance();
		
		if (sc.getLoggedInUser() instanceof Patient)
			setPanel(new PatientMainPanel());
		else if (sc.getLoggedInUser() instanceof Doctor || sc.getLoggedInUser() instanceof Nurse)
			setPanel(new DoctorNurseMainPanel());
		else if (sc.getLoggedInUser() instanceof SystemAdmin)
			setPanel(new SystemAdminMainPanel());
	}

}
